home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 02 / clipbrd / viewer4.c < prev   
Text File  |  1990-03-01  |  9KB  |  304 lines

  1. /*
  2.  * WINDOWS CLIPBOARD VIEWER - UTILITY SOURCE CODE
  3.  *
  4.  * LANGUAGE      : Microsoft C 5.1
  5.  * MODEL         : medium
  6.  * ENVIRONMENT   : Microsoft Windows 2.1 SDK
  7.  * STATUS        : operational
  8.  *
  9.  * 1.01- Kevin P. Welch - add param to GetClipboardFmtName.
  10.  *
  11.  */
  12.  
  13. #define  NOCOMM
  14.  
  15. #include <windows.h>
  16. #include "viewer.h"
  17.  
  18.  
  19. /*
  20.  * CenterPopup( hWnd, hParentWnd ) : BOOL
  21.  *
  22.  *   hWnd          window handle
  23.  *   hParentWnd      parent window handle
  24.  *
  25.  * This routine centers the popup window in the screen or display
  26.  * using the window handles provided.  The window is centered over
  27.  * the parent if the parent window is valid.  Special provision
  28.  * is made for the case when the popup would be centered outside
  29.  * the screen - in this case it is positioned at the appropriate
  30.  * border.
  31.  *
  32.  */
  33.  
  34. BOOL FAR PASCAL CenterPopup(
  35.    HWND    hWnd,
  36.    HWND    hParentWnd
  37.  )
  38. {
  39.  /* local variables */
  40.  int   xPopup;           /* popup x position */
  41.  int   yPopup;           /* popup y position */
  42.  int   cxPopup;          /* width of popup window */
  43.  int   cyPopup;          /* height of popup window */
  44.  int   cxScreen;         /* width of main display */
  45.  int   cyScreen;         /* height of main display */
  46.  int   cxParent;         /* width of parent window */
  47.  int   cyParent;         /* height of parent window */
  48.  RECT  rcWindow;         /* temporary window rect */
  49.  
  50.  /* retrieve main display dimensions */
  51.  cxScreen = GetSystemMetrics( SM_CXSCREEN );
  52.  cyScreen = GetSystemMetrics( SM_CYSCREEN );
  53.  
  54.  /* retrieve popup rectangle  */
  55.  GetWindowRect( hWnd, (LPRECT)&rcWindow );
  56.  
  57.  /* calculate popup extents */
  58.  cxPopup = rcWindow.right - rcWindow.left;
  59.  cyPopup = rcWindow.bottom - rcWindow.top;
  60.  
  61.  /* calculate bounding rectangle */
  62.  if ( hParentWnd ) {
  63.  
  64.    /* retrieve parent rectangle */
  65.    GetWindowRect( hParentWnd, (LPRECT)&rcWindow );
  66.  
  67.    /* calculate parent extents */
  68.    cxParent = rcWindow.right  - rcWindow.left;
  69.    cyParent = rcWindow.bottom - rcWindow.top;
  70.  
  71.    /* center within parent window */
  72.    xPopup = rcWindow.left + ((cxParent - cxPopup)/2);
  73.    yPopup = rcWindow.top  + ((cyParent - cyPopup)/2);
  74.  
  75.    /* adjust popup x-location for screen size */
  76.    if ( xPopup+cxPopup > cxScreen )
  77.      xPopup = cxScreen - cxPopup;
  78.  
  79.    /* adjust popup y-location for screen size */
  80.    if ( yPopup+cyPopup > cyScreen )
  81.      yPopup = cyScreen - cyPopup;
  82.  
  83.  } else {
  84.  
  85.    /* center within entire screen */
  86.    xPopup = (cxScreen - cxPopup) / 2;
  87.    yPopup = (cyScreen - cyPopup) / 2;
  88.  
  89.  }
  90.  
  91.  /* move window to new location & display */
  92.  MoveWindow(
  93.    hWnd,
  94.    ( xPopup > 0 ) ? xPopup : 0,
  95.    ( yPopup > 0 ) ? yPopup : 0,
  96.    cxPopup,
  97.    cyPopup,
  98.    TRUE
  99.  );
  100.  
  101.  /* normal return */
  102.  return( TRUE );
  103.  
  104. }
  105.  
  106.  
  107. /*
  108.  * GetClipboardFmtName( wFmt, lpszFmt, wMax, bInquire ) : WORD;
  109.  *
  110.  *    wFmt           clipboard format number
  111.  *    lpszFmt        name of clipboard format
  112.  *    wMax           maximum name size
  113.  *   bInquire      inquire full name if owner-display
  114.  *
  115.  * This utility function is identical to GetClipboardFormatName, but
  116.  * is capable of defining the name of ANY clipboard format, including
  117.  * predefined and owner-display ones.  Value returned is the number
  118.  * of bytes copied to the clipboard format name.  If this value is
  119.  * zero then the clipboard format number specified is undefined.
  120.  *
  121.  * Note that if the bInquire flag is TRUE, this function will attempt
  122.  * to ask the clipboard owner for the full name of the owner display
  123.  * data.  This may result in misleading information if this function
  124.  * is called with an owmer display format number when the real owner
  125.  * is not currently present!
  126.  *
  127.  */
  128.  
  129. WORD FAR PASCAL GetClipboardFmtName(
  130.  WORD      wFmt,
  131.  LPSTR     lpszFmt,
  132.  WORD      wMax,
  133.  BOOL      bInquire )
  134. {
  135.  HANDLE    hTemp;
  136.  LPSTR     lpszTemp;
  137.  
  138.  /* initialization */
  139.  lpszFmt[0] = 0;
  140.  
  141.  /* define format name */
  142.  switch( wFmt )
  143.    {
  144.  case NULL : /* empty */
  145.    lstrcpy( lpszFmt, "(Clipboard Empty)" );
  146.    break;
  147.  case CF_TEXT : /* standard text */
  148.    lstrcpy( lpszFmt, "Text" );
  149.    break;
  150.  case CF_BITMAP : /* standard GDI bitmap */
  151.    lstrcpy( lpszFmt, "Bitmap" );
  152.    break;
  153.  case CF_METAFILEPICT : /* standard GDI metafile */
  154.    lstrcpy( lpszFmt, "Picture" );
  155.    break;
  156.  case CF_SYLK : /* standard SYLK text */
  157.    lstrcpy( lpszFmt, "SYLK" );
  158.    break;
  159.  case CF_DIF : /* standard DIF text */
  160.    lstrcpy( lpszFmt, "DIF" );
  161.    break;
  162.  case CF_TIFF : /* standard binary TIFF data */
  163.    lstrcpy( lpszFmt, "TIFF" );
  164.    break;
  165.  case CF_OEMTEXT : /* standard OEM text */
  166.    lstrcpy( lpszFmt, "OEM Text" );
  167.    break;
  168.  case CF_OWNERDISPLAY : /* owner display */
  169.    lstrcpy( lpszFmt, "Owner Display" );
  170.    if ( bInquire && GetClipboardOwner() ) {
  171.      hTemp = GlobalAlloc( GHND, (DWORD)64 );
  172.      if ( hTemp ) {
  173.        lpszTemp = GlobalLock( hTemp );
  174.        if ( lpszTemp ) {
  175.          SendMessage( GetClipboardOwner(), WM_ASKCBFORMATNAME,
  176.                       63, (LONG)lpszTemp );
  177.          lstrcpy( lpszFmt, lpszTemp );
  178.          GlobalUnlock( hTemp );
  179.        }
  180.        GlobalFree( hTemp );
  181.      }
  182.    }
  183.    break;
  184.  case CF_DSPTEXT : /* display text */
  185.    lstrcpy( lpszFmt, "Display Text" );
  186.    break;
  187.  case CF_DSPBITMAP : /* display bitmap */
  188.    lstrcpy( lpszFmt, "Display Bitmap" );
  189.    break;
  190.  case CF_DSPMETAFILEPICT : /* display picture */
  191.    lstrcpy( lpszFmt, "Display Picture" );
  192.    break;
  193.  default : /* something else */
  194.    if ( GetClipboardFormatName(wFmt,lpszFmt,wMax) == 0 )
  195.      lstrcpy( lpszFmt, "(Undefined)" );
  196.    break;
  197.  }
  198.  
  199.  /* return size of string */
  200.  return( (WORD)lstrlen(lpszFmt) );
  201.  
  202. }
  203.  
  204. /*
  205.  * GetClipboardFmtNumber( lpszFmt ) : WORD;
  206.  *
  207.  *    lpszFmt        name of clipboard format
  208.  *
  209.  * This function retrieves (and if necessary, defines) the internal
  210.  * clipboard format number for the specified string.  Before checking
  211.  * registered clipboard formats, this function checks to see if one
  212.  * of the predefined formats is referenced.
  213.  *
  214.  */
  215.  
  216. WORD FAR PASCAL GetClipboardFmtNumber(
  217.  LPSTR       lpszFmt )
  218. {
  219.  WORD      wFmt;
  220.  
  221.  /* check predefined formats */
  222.  if ( lstrcmp(lpszFmt,"Text") == 0 )
  223.    wFmt = CF_TEXT;
  224.  else
  225.    if ( lstrcmp(lpszFmt,"Bitmap") == 0 )
  226.      wFmt = CF_BITMAP;
  227.    else
  228.      if ( lstrcmp(lpszFmt,"Picture") == 0 )
  229.        wFmt = CF_METAFILEPICT;
  230.      else
  231.        if ( lstrcmp(lpszFmt,"SYLK") == 0 )
  232.          wFmt = CF_SYLK;
  233.        else
  234.          if ( lstrcmp(lpszFmt,"DIF") == 0 )
  235.            wFmt = CF_DIF;
  236.          else
  237.            if ( lstrcmp(lpszFmt,"TIFF") == 0 )
  238.              wFmt = CF_TIFF;
  239.            else
  240.              if ( lstrcmp(lpszFmt,"OEM Text") == 0 )
  241.                wFmt = CF_OEMTEXT;
  242.              else
  243.                if ( lstrcmp(lpszFmt,"Owner Display") == 0 )
  244.                  wFmt = CF_OWNERDISPLAY;
  245.                else
  246.                  if ( lstrcmp(lpszFmt,"Display Text") == 0 )
  247.                    wFmt = CF_DSPTEXT;
  248.                  else
  249.                    if ( lstrcmp(lpszFmt,"Display Bitmap") == 0 )
  250.                      wFmt = CF_DSPBITMAP;
  251.                    else
  252.                      if ( lstrcmp(lpszFmt,"Display Picture") == 0 )
  253.                        wFmt = CF_DSPMETAFILEPICT;
  254.                      else
  255.                        wFmt = RegisterClipboardFormat( lpszFmt );
  256.  
  257.  /* return format */
  258.  return( wFmt );
  259.  
  260. }
  261.  
  262. /*
  263.  * GetClipboardModule( wCrntFmt, hLibrary ) : HANDLE;
  264.  *
  265.  *    wCrntFmt       current clipboard foramt
  266.  *    hLibrary       handle to clipboard support library
  267.  *
  268.  * This function searches the clipboard module library for one which
  269.  * can support the format in question.  If none is present a value of
  270.  * NULL is returned.  This should be interpreted that the current
  271.  * format is not supported by the system.
  272.  *
  273.  */
  274.  
  275. HANDLE FAR PASCAL GetClipboardModule(
  276.  WORD        wCrntFmt,
  277.  HANDLE      hLibrary )
  278. {
  279.  WORD      wEntry;
  280.  HANDLE    hModule;
  281.  LPLIBRARY lpLibrary;
  282.  
  283.  /* initialization */
  284.  hModule = NULL;
  285.  
  286.  lpLibrary = (LPLIBRARY)GlobalLock( hLibrary );
  287.  if ( lpLibrary ) {
  288.  
  289.    /* search library for module */
  290.    for ( wEntry=0;(wEntry<lpLibrary->wModules)&&(hModule==NULL);
  291.          wEntry++ )
  292.      if ( lpLibrary->Module[wEntry].wFormat == wCrntFmt )
  293.        hModule = lpLibrary->Module[wEntry].hModule;
  294.  
  295.    /* unlock library */
  296.    GlobalUnlock( hLibrary );
  297.  
  298.  }
  299.  
  300.  /* return module handle */
  301.  return( hModule );
  302.  
  303. }
  304.